<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Signrecord extends Model
{
    protected $table = "tp_sign_record";

    function mainData($size=10,$where=''){
        $map[] = ['p.id','<>',''];
        if(input('patient') && input('patient')<>''){
            $map[] = ['p.patient_id','like','%'.input('patient').'%'];
        }
        if(input('patient_name') && input('patient_name')<>''){
            $map[] = ['p.patient_name','like','%'.input('patient_name').'%'];
        }
        if(input('rfid') && input('rfid')<>''){
            $map[] = ['p.rfid','like','%'.input('rfid').'%'];
        }
        if(input('department_code') && input('department_code')<>''){
            $map[] = ['p.department_code','like','%'.input('department_code').'%'];
        }
        if(strlen(input('iswarn'))==1 ){
            if( input('iswarn') == 1 ){
                $map[] = ['r.iswarn','=',1];
            }else{
                $map[] = ['r.iswarn','=',0];
            }
        }
        if($where <> ''){
            $map = array_merge($where,$map);
        }
        // echo 3;
        $count = $this
                ->alias('r')
                ->join('patient p','p.rfid = r.xinpian','left')
                ->join('equipment e','e.equipment_code = r.addr','left')
                ->join('equip_group g','g.id=r.equipgroup_id','left')
                ->where($map)
                 ->count();// 查询满足要求的总记录数
        // echo 1;
        $list = $this
            ->alias('r')
            ->join('patient p','p.rfid = r.xinpian','left')
            ->join('equipment e','e.equipment_code = r.addr','left')
            ->join('equip_group g','g.id=r.equipgroup_id','left')
            ->field('p.patient_id,p.patient_name,p.gender,p.age,p.department,p.rfid,e.location_id,e.video_url,r.*,g.group_name as equipgroup_name')
            ->where($map)
            ->order('r.id desc')
            ->paginate($size,$count,['query'=>request()->param()])
            ->each(function($item,$key){
                $item['equipgroup_name'] = empty($item['equipgroup_name']) ? '普通告警' : $item['equipgroup_name'];
                return $item;
            });
        $page = $list->render();

        $data['count'] = $count;
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }

}